﻿using dz.u9.jky.schema.Common;
using dz.u9.jky.schema.Customer;
using dz.u9.jky.schema.Ship;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.Base.Organization;
using UFIDA.U9.CBO.FI.Tax;
using UFIDA.U9.CBO.SCM.Bin;
using UFIDA.U9.ISV.SM;
using UFIDA.U9.SM.Ship;
using CreateShipSV = UFIDA.U9.ISV.SM.CreateShipSV;
using ShipDTOForIndustryChain = UFIDA.U9.ISV.SM.ShipDTOForIndustryChain;
using ShipTaxDTOForIndustryChain = UFIDA.U9.ISV.SM.ShipTaxDTOForIndustryChain;

namespace dz.inner.jky.api.service
{
    internal class ShipService : BaseService
    {
        public U9CResult Add(DzShip dto)
        {
            return util.U9Invoke(__Add, dto,dto.Org);
        }

        public void SubmitAndAudit(CommonDoc doc)
        {
            ShipToApprove utils = new ShipToApprove();

            utils.Ship_ID = new List<long>() { doc.ID };

            utils.Do();
        }

        public U9CResult __Add(DzShip ship)
        {
            U9CResult rtn = new U9CResult();

            try
            {
                CreateShipSV sv = new CreateShipSV();

                sv.ShipDTOs = Convert(ship);

                List<DocKeyDTO> aa = sv.Do();

                rtn.IsSuccess = true;
                rtn.DocNo = aa[0].DocNO;
                rtn.Id = aa[0].DocID;

            }
            catch (Exception ex)
            {
                rtn.IsSuccess = false;
                logUtils.Error(ex);
                rtn.Message = ex.Message;
            }

            return rtn;
        }

        private List<ShipDTOForIndustryChain> Convert(DzShip ship)
        {
            ObjectCopyUtil copyUtil = new ObjectCopyUtil();

            ShipDTOForIndustryChain obj = new ShipDTOForIndustryChain();

            //dynamic c = (dynamic)obj;
            copyUtil.CopyProperty(ship, ref obj);

            string where = $"code = '{ship.Org}'";
            long org = Organization.Finder.Find(where).ID;

            if (obj.ShipLines != null && obj.ShipLines.Count > 0)
            {
                foreach (UFIDA.U9.ISV.SM.ShipLineDTOForIndustryChain line in obj.ShipLines)
                {
                    if (line.TaxSchedule != null)
                    {
                        TaxSchedule schedule = TaxSchedule.Finder.Find($"code = '{line.TaxSchedule.Code}'");
                        if (schedule != null)
                        {
                            decimal taxRate = schedule.TaxScheduleTaxs[0].Tax.TaxRate;
                            if (obj.IsPriceIncludeTax)
                            {
                                decimal taxAmt = line.FinallyPriceTC * line.ShipQtyInvAmount;
                                decimal amt = Math.Round(taxAmt / (1 + taxRate), 2);
                                decimal tax = taxAmt - amt;

                                line.TotalNetMoneyTC = amt;
                                line.TotalNetMoneyFC = amt;
                                line.TotalNetMoney = amt;
                                line.TotalTaxTC = tax;
                                line.TotalTaxFC = tax;
                                line.TotalTax = tax;
                                line.TaxRate = taxRate;

                                if (line.ShipTaxs == null) line.ShipTaxs = new List<ShipTaxDTOForIndustryChain>();

                                ShipTaxDTOForIndustryChain shipTax;
                                if (line.ShipTaxs.Count == 0)
                                {
                                    shipTax = new ShipTaxDTOForIndustryChain();
                                    shipTax.Org = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTO() { Code = ship.Org };
                                    shipTax.TaxRate = taxRate;
                                    shipTax.TaxAmount = tax;
                                    shipTax.TaxAmountFC = tax;
                                    shipTax.Tax = new UFIDA.U9.CBO.Pub.Controller.CommonArchiveDataDTO() { Code = schedule.TaxScheduleTaxs[0].Tax.Code };
                                    line.ShipTaxs.Add(shipTax);
                                }
                                else 
                                {
                                    shipTax = line.ShipTaxs[0];
                                    shipTax.TaxAmount = tax;
                                    shipTax.TaxAmountFC = tax;
                                }
                            }
                        }
                        
                    }
                    foreach (var location in line.ShipLineLocations)
                    {

                        Bin bin = Bin.Finder.Find($"code = '{location.LocationCode}' and org = {org}");
                        if (bin == null)
                        {
                            throw new Exception($"组织{ship.Org}中 找不到库位：{location.LocationCode}");
                        }
                        location.Location.ID = bin.ID;
                    }
                }
            }

            List<ShipDTOForIndustryChain> rtn = new List<ShipDTOForIndustryChain>();

            rtn.Add(obj);

            return rtn;
        }


    }
}
